home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Symbol.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  2.6 KB  |  98 lines

  1. package Symbol;
  2.  
  3. =head1 NAME
  4.  
  5. Symbol - manipulate Perl symbols and their names
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.     use Symbol;
  10.  
  11.     $sym = gensym;
  12.     open($sym, "filename");
  13.     $_ = <$sym>;
  14.  
  15.     ungensym $sym;      # no effect
  16.  
  17.     print qualify("x"), "\n";              # "Test::x"
  18.     print qualify("x", "FOO"), "\n"        # "FOO::x"
  19.     print qualify("BAR::x"), "\n";         # "BAR::x"
  20.     print qualify("BAR::x", "FOO"), "\n";  # "BAR::x"
  21.     print qualify("STDOUT", "FOO"), "\n";  # "main::STDOUT" (global)
  22.     print qualify(\*x), "\n";              # returns \*x
  23.     print qualify(\*x, "FOO"), "\n";       # returns \*x
  24.  
  25.     use strict refs;
  26.     print { qualify_to_ref $fh } "foo!\n";
  27.     $ref = qualify_to_ref $name, $pkg;
  28.  
  29. =head1 DESCRIPTION
  30.  
  31. C<Symbol::gensym> creates an anonymous glob and returns a reference
  32. to it.  Such a glob reference can be used as a file or directory
  33. handle.
  34.  
  35. For backward compatibility with older implementations that didn't
  36. support anonymous globs, C<Symbol::ungensym> is also provided.
  37. But it doesn't do anything.
  38.  
  39. C<Symbol::qualify> turns unqualified symbol names into qualified
  40. variable names (e.g. "myvar" -E<gt> "MyPackage::myvar").  If it is given a
  41. second parameter, C<qualify> uses it as the default package;
  42. otherwise, it uses the package of its caller.  Regardless, global
  43. variable names (e.g. "STDOUT", "ENV", "SIG") are always qualfied with
  44. "main::".
  45.  
  46. Qualification applies only to symbol names (strings).  References are
  47. left unchanged under the assumption that they are glob references,
  48. which are qualified by their nature.
  49.  
  50. C<Symbol::qualify_to_ref> is just like C<Symbol::qualify> except that it
  51. returns a glob ref rather than a symbol name, so you can use the result
  52. even if C<use strict 'refs'> is in effect.
  53.  
  54. =cut
  55.  
  56. BEGIN { require 5.002; }
  57.  
  58. require Exporter;
  59. @ISA = qw(Exporter);
  60. @EXPORT = qw(gensym ungensym qualify qualify_to_ref);
  61.  
  62. $VERSION = 1.02;
  63.  
  64. my $genpkg = "Symbol::";
  65. my $genseq = 0;
  66.  
  67. my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT);
  68.  
  69. sub gensym () {
  70.     my $name = "GEN" . $genseq++;
  71.     my $ref = \*{$genpkg . $name};
  72.     delete $$genpkg{$name};
  73.     $ref;
  74. }
  75.  
  76. sub ungensym ($) {}
  77.  
  78. sub qualify ($;$) {
  79.     my ($name) = @_;
  80.     if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) {
  81.     my $pkg;
  82.     if ($name =~ /^([^a-z])|(\^[a-z])$/i || $global{$name}) {
  83.         $pkg = "main";
  84.     }
  85.     else {
  86.         $pkg = (@_ > 1) ? $_[1] : caller;
  87.     }
  88.     $name = $pkg . "::" . $name;
  89.     }
  90.     $name;
  91. }
  92.  
  93. sub qualify_to_ref ($;$) {
  94.     return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
  95. }
  96.  
  97. 1;
  98.